package com.highglass.videoplayer.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by wangjianqiang on 2017/11/2.
 */

public class GetHistoryReceiver extends BroadcastReceiver {
    private static final String GET_HISTORY = "com.highglass.videoplayer.gethisotry";
    private static final String DELETE_HISTORY = "com.highglass.videoplayer.deletehisotry";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(GET_HISTORY)) {
            File fromFile = new File(context.getFilesDir().getParent() + "/databases/file_manager.db");
            if (fromFile.exists()) {
                File toFile = new File(Environment.getExternalStorageDirectory() + "/file_manager.db");
                if (toFile.exists()) {
                    toFile.delete();
                }
                if (CopySdcardFile(fromFile.getPath(), Environment.getExternalStorageDirectory() + "/file_manager.db")) {
                    fromFile.delete();
                }
            }
        }
        if (action.equals(DELETE_HISTORY)) {
            File file = new File(Environment.getExternalStorageDirectory() + "/file_manager.db");
            if (file.exists()) {
                deleteFiles(file);
            }
            File history_file = new File(Environment.getExternalStorageDirectory() + "/history_time.txt");
            String time = intent.getStringExtra("time");
            if (history_file.exists()) {
                deleteFiles(file);
            }
            try {
                history_file.createNewFile();
                FileOutputStream outputStream = new FileOutputStream(history_file);
                outputStream.write(time.getBytes());
                outputStream.close();
            } catch (Exception e) {

            }
        }

    }

    public boolean deleteFiles(File file) {
        if (file == null) {
            return false;
        }
        if (file.isFile()) {
            return file.delete();
        }
        File[] files = file.listFiles();
        if (files == null) {
            return false;
        }
        for (int i = 0; i < files.length; i++) {
            File childFile = files[i];
            if (childFile.isFile()) {
                childFile.delete();
            } else {
                deleteFiles(childFile);
            }
        }
        return file.delete();
    }

    public boolean CopySdcardFile(String fromFile, String toFile) {
        InputStream fosfrom = null;
        OutputStream fosto = null;
        try {
            fosfrom = new FileInputStream(fromFile);
            File file = new File(toFile);
            if (!file.createNewFile()) {
                return false;
            }
            fosto = new FileOutputStream(file);
            byte bt[] = new byte[1024 * 1024];
            int c;
            while ((c = fosfrom.read(bt)) != -1) {
                fosto.write(bt, 0, c);
            }
            fosfrom.close();
            fosto.close();
            return true;

        } catch (Exception ex) {
            Log.e("fuck you", ex.toString() + "sssss");
            try {
                fosfrom.close();
                fosto.close();
            } catch (Exception e) {

            }
            return false;
        }
    }
}
